package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.ItemCatMapper;
import com.jt.pojo.ItemCat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.yaml.snakeyaml.events.Event;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author :Colin
 * @version 创建时间: 2021/8/8 15:41
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private ItemCatMapper itemCatMapper;

    @Override
    public List<ItemCat> findItemCatList(Integer level) {
        long startTime = System.currentTimeMillis();
        List<ItemCat> itemCatList = itemCatMapper.selectList(null);
        HashMap<Integer, List<ItemCat>> map= new HashMap<>();
        for(ItemCat itemCat :itemCatList){
            Integer key = itemCat.getParentId();
            if(map.containsKey(key)){
                map.get(key).add(itemCat);
            }else{
                List<ItemCat> list = new ArrayList<>();
                list.add(itemCat);
                map.put(key, list);
            }
        }
        if(level == 1){
            return map.get(0);
        }
        if(level == 2){
            return getLevel2List(map);
        }

        long endTime = System.currentTimeMillis();
        System.out.println("所有时间"+(endTime-startTime)+"毫秒");
        return  getLevel3List(map);
    }



    public List<ItemCat> getLevel3List(HashMap<Integer, List<ItemCat>> map) {
        List<ItemCat> oneList = getLevel2List(map);
        for(ItemCat oneItemCat:oneList){
            List<ItemCat> twoList = oneItemCat.getChildren();
            if(twoList ==null || twoList.size() == 0){
                continue;
            }
            //说明该元素有二级,.查询三级
            for(ItemCat twoItemCat:twoList){
                List<ItemCat> threeList = map.get(twoItemCat.getId());
                twoItemCat.setChildren(threeList);
            }

        }

        return oneList;
    }

    public List<ItemCat> getLevel2List(HashMap<Integer, List<ItemCat>> map) {
        List<ItemCat> oneList = map.get(0);
        for (ItemCat oneItemCat:oneList){
            List<ItemCat> twoList = map.get(oneItemCat.getId());
            oneItemCat.setChildren(twoList);
        }
        return oneList;
    }



    /*@Override
    public List<ItemCat> findItemCatList(Integer level) {
        long startTime = System.currentTimeMillis();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("parent_id", 0);
        List<ItemCat> oneList = itemCatMapper.selectList(queryWrapper);
        for(ItemCat oneItemCat:oneList){
            queryWrapper.clear();
            queryWrapper.eq("parent_id", oneItemCat.getId());
            List<ItemCat> twoList = itemCatMapper.selectList(queryWrapper);
            for(ItemCat twoItemCat:twoList){
                queryWrapper.clear();
                queryWrapper.eq("parent_id", twoItemCat.getId());
                List<ItemCat> threeList = itemCatMapper.selectList(queryWrapper);
                twoItemCat.setChildren(threeList);
            }
            oneItemCat.setChildren(twoList);
        }
        long endTime = System.currentTimeMillis();
        System.out.println("所有时间"+(endTime-startTime)+"毫秒");
        return oneList;

    }*/

    @Override
    @Transactional
    public void updateItemStatus(ItemCat itemCat) {
        itemCatMapper.updateById(itemCat);
    }

    @Override
    @Transactional
    public void saveItemCat(ItemCat itemCat) {
        itemCat.setStatus(true);
        itemCatMapper.insert(itemCat);
    }

    @Override
    @Transactional
    public void deleteItemCat(Integer id, Integer level) {
        if(level == 3){
            itemCatMapper.deleteById(id);
        }
        if(level == 2){
            /*方法一*/
            /*QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", id);
            List<ItemCat> threeList = itemCatMapper.selectList(queryWrapper);
            for(ItemCat threeItemCat:threeList){
                itemCatMapper.deleteById(threeItemCat.getId());
            }
            itemCatMapper.deleteById(id);*/
            /*方法二*/
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id",id);
            itemCatMapper.delete(queryWrapper);
            itemCatMapper.deleteById(id);

        }
        /*方法一*/

            /* if(level == 1){
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", id);
            List<ItemCat> twoList = itemCatMapper.selectList(queryWrapper);
            for(ItemCat twoItemCat:twoList){
                queryWrapper.clear();
                queryWrapper.eq("parent_id", twoItemCat.getId());
                List<ItemCat> threeList = itemCatMapper.selectList(queryWrapper);
                for(ItemCat threeItemCat:threeList){
                    itemCatMapper.deleteById(threeItemCat.getId());
                }
                itemCatMapper.deleteById(twoItemCat.getId());
            }
            itemCatMapper.deleteById(id);
            }*/

            /*方法二:普通版*/
        /*if(level == 1){
            QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("parent_id", id);
            List<ItemCat> twoList = itemCatMapper.selectList(queryWrapper);
            *//*可能有空数组的情况,但是没关系,空数组不会遍历*//*
            for(ItemCat twoItemCat:twoList){
                *//*1.根据二级删除三级*//*
                queryWrapper.clear();
                queryWrapper.eq("parent_id", twoItemCat.getId());
                //delete from item_cat where parent_id ="二级id"
                itemCatMapper.delete(queryWrapper);
                *//*2.删除二级*//*
                itemCatMapper.deleteById(twoItemCat.getId());
            }
            itemCatMapper.deleteById(id);

        }*/
            /*方法三*/
            if(level == 1){
                List<Integer> ids = new ArrayList<>();
                QueryWrapper<ItemCat> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("parent_id", id);
                List<ItemCat> twoList = itemCatMapper.selectList(queryWrapper);

                for(ItemCat twoItemCat:twoList){

                    queryWrapper.clear();
                    queryWrapper.eq("parent_id", twoItemCat.getId());

                    itemCatMapper.delete(queryWrapper);
                    ids.add(twoItemCat.getId());

                }
                ids.add(id);
                itemCatMapper.deleteBatchIds(ids);

            }
    }
}
